package com.oinux.playhelper.app;

import com.oinux.playhelper.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class BaseActivity extends Activity{

	public static final String KEY_THEME_ID = "theme_id";

	protected SharedPreferences preferences;
	
	protected AppContext appContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		appContext = (AppContext) getApplication();
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		int id = preferences.getInt(KEY_THEME_ID, -1);
		if (id != -1) {
			setTheme(id);
		}
		appContext.addActivity(this); 
	}
	
	protected int getThemeId(){
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		int id = preferences.getInt(KEY_THEME_ID, R.style.qinxin);
		return id;
	}
	
	protected void switchTheme(){
		int id = preferences.getInt(KEY_THEME_ID, R.style.qinxin);
		if(id == R.style.qinxin){
			id = R.style.wenzhong;
		}else{
			id = R.style.qinxin;
		}
		appContext.setSwitchTheme(true); 
		preferences.edit().putInt(KEY_THEME_ID, id).commit();
		restart();
	}
	
	protected void switchTheme(int theme){
		appContext.setSwitchTheme(true); 
		preferences.edit().putInt(KEY_THEME_ID, theme).commit();
		restart();
	}
	
	protected void restart(){
		finish();
		overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		startActivity(new Intent(this, this.getClass()));
	}
}
